/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sh.ndt.user.endpoint;

import com.sh.ndt.common.entity.EmailContent;
import com.sh.ndt.common.service.EmailService;
import com.sh.ndt.exception.ServiceException;
import com.sh.ndt.user.entity.User;
import com.sh.ndt.user.entity.UserParam;
import com.sh.ndt.user.service.UserEntitlementService;
import com.sh.ndt.user.service.UserService;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Prashanth
 */
@Controller
@RequestMapping("/userService")
public class UserEndpoint {
    private static final Logger LOGGER = Logger.getLogger(UserEndpoint.class);

    public UserEndpoint(){
        LOGGER.debug("UserEndpoint is initialized ********************** ");
    }    
    @Autowired
    UserService userService;    
    
    @Autowired
    EmailService emailService;    
    
    
    @Autowired
    UserEntitlementService userEntitlementService;
    
    @RequestMapping(value = "/addEditUser",
            method = RequestMethod.POST,
            consumes = "application/xml",
            produces = "application/json"
    )
    @ResponseBody
    public User addEditUser(@RequestBody UserParam userParam) {
        try {
            
            LOGGER.debug("Users Role Id: " + userParam.getUserRole());
            LOGGER.debug("Users Type: " + userParam.getUserType());
            
            User userDto = new User();
            BeanUtils.copyProperties(userParam, userDto);
            
            List<User> userList = userService.addEditUser(userDto);
            LOGGER.debug("Users First Name: " + userList.get(0).getFirstName());
            LOGGER.debug("Users Last Name: " + userList.get(0).getLastName());
            
            return userList.get(0);
        } catch (ServiceException ex) {
            LOGGER.debug(ex);
        }
        return null;
    }
    
    @RequestMapping(value = "/sendNotificationToUser",
            method = RequestMethod.POST,
            consumes = "application/xml"
    )
    @ResponseBody
    public boolean sendNotificationToUser(@RequestBody EmailContent emailContent){
        try{
            emailService.sendEmail(emailContent);
            return true;
        }catch (ServiceException ex){
            LOGGER.debug(ex);
        }
        return false;
    }

}
